home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Online / NNTPd / server / fakesyslog.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-12-09  |  3.1 KB  |  190 lines

  1. #ifndef lint
  2. static char    sccsid[] = "@(#)$Id: fakesyslog.c,v 1.9 1994/12/09 02:52:18 sob Exp sob $";
  3. #endif
  4.  
  5. /*
  6.  * Fake syslog routines for systems that don't have syslog.
  7.  * Taken from an idea by Paul McKenny, <mckenny@sri-unix.arpa>.
  8.  * (Unfortunately, Paul, I can't distribute the real syslog code
  9.  * as you suggested ... sigh.)
  10.  *
  11.  * Warning: this file contains joe code that may offend you.
  12.  */
  13.  
  14. #include "../conf.h"
  15.  
  16. #ifdef FAKESYSLOG
  17.  
  18. #include "fakesyslog.h"
  19.  
  20. #include <stdio.h>
  21. #include <sys/signal.h>
  22. #include <sys/types.h>
  23.  
  24. #ifdef FAKEAPPEND
  25. #include <sys/file.h>
  26. #endif
  27.  
  28. #ifdef FCNTL
  29. #include <fcntl.h>
  30. #endif
  31.  
  32. extern    int    errno;
  33. extern    int    sys_nerr;
  34. extern    char    *sys_errlist[];
  35.  
  36. static FILE    *logfp;
  37. static int    failed = 0;
  38. static char    *ident = "syslog";
  39. static int     opt = 0;
  40. #ifdef LOG_DAEMON
  41. static int    fac = 0;
  42. #endif
  43.  
  44. extern char    *strcpy(), *strcat(), *ctime();
  45. extern time_t    time();
  46.  
  47. SIGRET
  48. resetlog()
  49. {
  50.     closelog();
  51.     failed = 0;
  52.     if (logfp == NULL) {
  53. #ifdef LOG_DAEMON
  54.         openlog(ident, opt, fac);
  55. #else
  56.         openlog(ident, opt);
  57. #endif
  58.         if (logfp == NULL)
  59.             failed = 1;
  60.     }
  61.     (void)signal(SIGHUP, resetlog);
  62. #ifndef VOIDSIG
  63.     return 0;
  64. #endif
  65. }
  66.  
  67. #ifdef LOG_DAEMON
  68. openlog(newident,logopt,facility)
  69.     char *newident;
  70.     int logopt, facility;
  71. #else
  72. openlog(newident,logopt)
  73.     char *newident;
  74.     int logopt;
  75. #endif
  76. {
  77. #ifdef FAKEAPPEND
  78. /*
  79.  * why can't stdio give us the capability of O_APPEND?
  80.  */
  81.     int fd;
  82.  
  83.     fd = open(FAKESYSLOG, O_WRONLY|O_APPEND, 0664);
  84.     if (fd < 0)
  85.         logfp = NULL;
  86.     else
  87.         logfp = fdopen(fd, "a");
  88. #else
  89.     logfp = fopen(FAKESYSLOG, "a");
  90. #endif
  91.  
  92.     (void)signal(SIGHUP, resetlog);
  93.  
  94.     if (newident && *newident)
  95.         ident = newident;
  96.     opt = logopt;
  97. #ifdef LOG_DAEMON
  98.     fac = facility;
  99. #endif
  100. }
  101.  
  102. closelog()
  103. {
  104.     if (logfp) {
  105.         (void)fclose(logfp);
  106.         failed = 0;
  107.         logfp = NULL;
  108.     }
  109. }
  110.  
  111. /*ARGSUSED*/
  112. setlogmask(maskpri)
  113.     int maskpri;
  114. {
  115. }
  116.  
  117. syslog(pri, msg, x1, x2, x3, x4, x5, x6)
  118.     int    pri;
  119.     char    *msg, *x1, *x2, *x3, *x4, *x5, *x6;
  120. {
  121.     char        buf[1024];
  122.     char        *cp, *bp;
  123.     time_t        clock;
  124.  
  125.     if (failed)
  126.         return;
  127.  
  128.     if (logfp == NULL) {
  129. #ifdef LOG_DAEMON
  130.         openlog(ident, opt, fac);
  131. #else
  132.         openlog(ident, opt);
  133. #endif
  134.         if (logfp == NULL) {
  135.             failed = 1;
  136.             return;
  137.         }
  138.     }
  139.  
  140.     (void) time(&clock);
  141.     (void) strcpy(buf, ctime(&clock)+4);
  142.     *(bp = buf + 16) = '\0';
  143.  
  144. #if 0
  145.     (void) sprintf(bp, "localhost %s", ident ? ident : "");
  146. #else
  147.     (void) sprintf(bp, "local %s", ident ? ident : "");
  148. #endif
  149.     bp += strlen(bp);
  150.  
  151.     if (opt&LOG_PID) {
  152.         /* don't cache getpid() - who knows when we'll fork() */
  153.         (void) sprintf(bp, "[%d]", getpid());
  154.         bp += strlen(bp);
  155.     }
  156.  
  157.     if (ident) {
  158.         (void) strcat(bp, ": ");
  159.         bp += 2;
  160.     } else {
  161.         (void) strcat(bp, " ");
  162.         bp ++;
  163.     }
  164.  
  165.     for (cp = msg; *cp; cp++) {
  166.         if (*cp == '%' && cp[1] == 'm') {
  167.             *bp = '\0';
  168.             if (errno >= sys_nerr || errno < 0) {
  169.                 char    work[32];
  170.                 (void)sprintf(work, "unknown error #%d", errno);
  171.                 (void)strcat(bp, work);
  172.             } else
  173.                 (void)strcat(bp, sys_errlist[errno]);
  174.             bp = buf + strlen(buf);
  175.             cp++;
  176.         } else {
  177.             *bp++ = *cp;
  178.         }
  179.     }
  180.     *bp = '\0';
  181.     /* Ah, the semantic security of C ... */
  182.     if (bp[-1] != '\n')
  183.         (void) strcat(bp, "\n");
  184.  
  185.     fprintf(logfp, buf, x1, x2, x3, x4, x5, x6);
  186.     (void) fflush(logfp);
  187. }
  188.  
  189. #endif
  190.